package com.superzqb.asus.superzqbFragment.superzqbShopMain.Shop;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.baoyz.widget.PullRefreshLayout;
import com.bigkoo.convenientbanner.ConvenientBanner;
import com.superzqb.asus.Api.ShopApi;
import com.superzqb.asus.superzqb.R;
import com.superzqb.asus.superzqbFragment.superzqbShopMain.Adapter.GoodsDetailAdapter;
import com.superzqb.asus.superzqbFragment.superzqbShopMain.Bean.GoodsDetailBean;
import com.superzqb.asus.superzqbUtil.GeneralFun;
import com.superzqb.asus.superzqbUtil.Url;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static com.superzqb.asus.Api.ApiParam.imgUrl;

public class GoodsDetail extends Activity implements View.OnClickListener{

  /*  private String[] images = {"http://img5.ph.126.net/6pJFXYVpdfwE2c7tiETnFA==/1011339591338529305.jpg",
            "http://cs.vmovier.com/Uploads/post/2015-07-22/55af41c4a407f.jpg",
            "http://img3.fengniao.com/travel/2_960/1614.jpg",
            "http://img1.gamedog.cn/2012/10/30/24-1210301422250-51.jpg",
            "http://pic1.win4000.com/wallpaper/6/55adfc93a238b.jpg",
            "http://image69.360doc.com/DownloadImg/2014/03/0414/39605721_8.jpg",
            "http://a3.att.hudong.com/20/03/300000868465128982039471344_950.jpg"
    };*/
    private List<String> images=new ArrayList<>();
    private Context context;
    private LinearLayout li;
    private ViewGroup.LayoutParams i;
    private ConvenientBanner convenient;//顶部广告栏控件
    private ImageView back;
    private ListView lv;
    int id;
    private PullRefreshLayout layout;
    private List<GoodsDetailBean> listAll;
    private GoodsDetailAdapter adapter;
    private int lastItem;
    private int listSize;
    private int pagenum=1;
    private TextView title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.goods_detail);
        Intent intent=this.getIntent();
        id=Integer.valueOf(intent.getStringExtra("id")) ;

        listAll=new ArrayList<>();
        images.addAll((List<String>)intent.getSerializableExtra("images"));
        initView();
        title.setText(intent.getStringExtra("name"));
        adapter = new GoodsDetailAdapter(GoodsDetail.this, listAll);
        lv.setAdapter(adapter);
        getLv();
        layout.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                layout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        layout.setRefreshing(false);
                        mClear();
                        getLv();
                    }
                }, 2000);
            }
        });

        //Toast.makeText(GoodsDetail.this,intent.getStringExtra("id"),Toast.LENGTH_SHORT).show();
        //设置轮播图
        for(int i=0;i<images.size();i++) {
            images.set(i,imgUrl + images.get(i));
        }
        new GeneralFun().setCarousel1(GoodsDetail.this,convenient,images,i.width,i.height);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent,View view,int position,long id){
                // TODO Auto-generated method stub
                adapter.changeColor(position);
                adapter.notifyDataSetInvalidated();
                GoodsDetailBean item=(GoodsDetailBean)parent.getItemAtPosition(position);
                Intent intent=new Intent(context,GoodsContent.class);
                Bundle bundle=new Bundle();
                bundle.putSerializable("map",item);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        lv.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == 0) {
                    if (lastItem == listSize) {
                        pagenum++;
                      /*  Toast.makeText(GoodsDetail.this,
                                "加载第" + pagenum + "页",
                                Toast.LENGTH_SHORT).show();*/
                        //getLv();
                    }
                }
            }
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                                 int visibleItemCount, int totalItemCount) {
                lastItem = lv.getLastVisiblePosition();

            }
        });

    }

    private void initView() {
        context=GoodsDetail.this;
        layout= (PullRefreshLayout)findViewById(R.id.swipeRefreshLayout);
        back=(ImageView)findViewById(R.id.back);
        li=(LinearLayout)findViewById(R.id.play);
        lv=(ListView)findViewById(R.id.listView);
        title=(TextView)findViewById(R.id.title);
        convenient = (ConvenientBanner)findViewById(R.id.convenientBanner);
        Display d=GoodsDetail.this.getWindowManager().getDefaultDisplay();
        /*获取布局参数*/
        i=li.getLayoutParams();
        i.height=d.getHeight()/3;
        i.width=d.getWidth();
        back.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
            default:break;
        }

    }
    public void getLv() {
        Retrofit retrofit= new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(new Url().getBase_url())
                .build();
        ShopApi api=retrofit.create(ShopApi.class);
        Call<List<GoodsDetailBean>> call=api.getGoodsList(pagenum,id);
        call.enqueue(new Callback<List<GoodsDetailBean>>() {
            @Override
            public void onResponse(Call<List<GoodsDetailBean>> call, Response<List<GoodsDetailBean>> response) {
                if(response.isSuccessful()){
                    List<GoodsDetailBean> list=response.body();
                    if(list!=null) {
                        listAll.addAll(list);
                        listSize = listAll.size() - 1;
                        adapter.setList(listAll);
                        adapter.notifyDataSetChanged();
                    }/*else{
                        ;
                    }*/
                }

            }

            @Override
            public void onFailure(Call<List<GoodsDetailBean>> call, Throwable throwable) {
              //  Toast.makeText(GoodsDetail.this,"刷新出错",Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();

        adapter.setList(listAll);
        adapter.notifyDataSetChanged();
    }
    private void mClear() {
        listAll.clear();
        listSize=0;
        pagenum=1;
    }
}
